<template>
    <div>
        <NuxtLayout name="common">
            <div class="box mt-[30px]">
                <t-card title="套包一：夏长裤 165cm * 2 、T恤 165cm *2 、 运动短裤 150cm * 3  |  套包二：寸衫 175cm * 3 、网袜 * 20">
                    <t-form>
                        <t-form-item label="商品选择">
                            <t-checkbox-group v-model="orderInfo.goods_list">
                                <t-checkbox key="all" :check-all="true" label="全选" />
                                <t-checkbox key="Txu" value="套包一">套包一</t-checkbox>
                                <t-checkbox key="Txu" value="套包二">套包二</t-checkbox>
                                <t-checkbox key="Txu" value="运动短裙">运动短裙</t-checkbox>
                                <t-checkbox key="Txu" value="内胆">内胆</t-checkbox>
                            </t-checkbox-group>
                        </t-form-item>
                        <t-form-item label="姓名">
                            <t-input v-model="orderInfo.content.username" placeholder="请输入姓名"></t-input>
                        </t-form-item>
                        <t-form-item label="电话">
                            <t-input v-model="orderInfo.content.phone" placeholder="请输入电话号码"></t-input>
                        </t-form-item>
                        <t-form-item label="收货地址">
                            <t-input v-model="orderInfo.content.address" placeholder="请输入收货地址"></t-input>
                        </t-form-item>
                        <t-form-item>
                            <t-button v-show="isOpen" @click="clickSubmitOrder">提交订单</t-button>
                            <t-button theme="default" @click="endSales">截止预售</t-button>
                        </t-form-item>
                    </t-form>
                </t-card>
            </div>
        </NuxtLayout>
    </div>
</template>

<script setup lang="ts">
import { MessagePlugin } from 'tdesign-vue-next';
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';

// 使用路由
const router = useRouter();

// 订单标
const orderTable = ref([]);

// 零售收货人信息
const orderInfo = reactive({
    content: {
        username: '张三',
        phone: '13800138000',
        address: '重庆市北碚区蔡家岗街道倍都佳园D区',
        school: '重庆市渝北区礼嘉中学'
    },
    goods_list: [],
    sale: {
        order_number: '',
        order_time: '',
        order_from: ''
    },
    out_warehouse: {
        warehouse_name: '',
        out_number: '',
        out_time: '',
        express_number: '',
    }
})

// 是否开启预售
const isOpen = ref(true);

// 点击提交订单按钮
const clickSubmitOrder = () => {
    // console.log(orderInfo.goods_list);return;
    let date = new Date();
    let numberPrefix = `${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}`

    // 判断商品列表
    if (orderInfo.goods_list.length < 1) {
        MessagePlugin.error({
            content: '必须选择商品'
        })
        return;
    }
    // 判断用户信息
    if (orderInfo.content.username === '') {
        MessagePlugin.error({
            content: '收货人不能为空'
        })
        return;
    }
    if (orderInfo.content.phone === '') {
        MessagePlugin.error({
            content: '联系电话不能为空'
        })
        return;
    }
    if (orderInfo.content.address === '') {
        MessagePlugin.error({
            content: '收货地址不能为空'
        })
        return;
    }

    // 获取本地零散订单信息
    let orderTables = localStorage.getItem('orderTables');
    // 所有本地商品map
    let goodsMaps = localStorage.getItem('goodsMaps');
    if (orderTables === null) { // 新建第一个订单
        // 判断套包还是单品
        goodsMaps = [];
        let tmpOrdersTable = {
            user: {
                username: orderInfo.content.username,
                phone: orderInfo.content.phone,
                school: orderInfo.content.school,
                address: orderInfo.content.address
            },
            goods_list: []
        };
        orderTables = [];
        orderInfo.goods_list.forEach(item => {
            // 组装goodsMaps
            if (item === '套包一') {
                goodsMaps.push({
                    goods_title: '夏长裤 165cm',
                    number: 2,
                    warehouse: '',
                    out_number: '',
                    out_time: '',
                    express_number: '',
                },
                    {
                        goods_title: 'T恤 165cm',
                        number: 2,
                        warehouse: '',
                        out_number: '',
                        out_time: '',
                        express_number: ''
                    },
                    {
                        goods_title: '运动短裤 150cm',
                        number: 3,
                        warehouse: '',
                        out_number: '',
                        out_time: '',
                        express_number: ''
                    });
            } else if (item === '套包二') {
                goodsMaps.push({
                    goods_title: '寸衫 175cm',
                    number: 3,
                    warehouse: '',
                    out_number: '',
                    out_time: '',
                    express_number: ''
                },
                    {
                        goods_title: '网袜',
                        number: 20,
                        warehouse: '',
                        out_number: '',
                        out_time: '',
                        express_number: ''
                    })
            } else {
                goodsMaps.push({
                    goods_title: item,
                    number: 10,
                    warehouse: '',
                    out_number: '',
                    out_time: '',
                    express_number: ''
                })
            }

            // 组装用户订单信息
            if (item === '套包一') {
                tmpOrdersTable.goods_list.push(
                    {
                        goods_title: '夏长裤 165cm',
                        number: 2
                    },
                    {
                        goods_title: 'T恤 165cm',
                        number: 2
                    },
                    {
                        goods_title: '运动短裤',
                        number: 3
                    }
                )
            } else if (item === '套包二') {
                tmpOrdersTable.goods_list.push(
                    {
                        goods_title: '寸衫 175cm',
                        number: 3
                    },
                    {
                        goods_title: '网袜',
                        number: 20
                    }
                )
            } else {
                tmpOrdersTable.goods_list.push(
                    {
                        goods_title: item,
                        number: 10
                    }
                )
            }
        });
        orderTables.push(tmpOrdersTable)


        // 写入本次预售的商品品类及汇总MAP
        localStorage.setItem('goodsMaps', JSON.stringify(goodsMaps));
        // 写入用户订单表
        localStorage.setItem('orderTables', JSON.stringify(orderTables));

    } else { // 已经存在订单
        // 格式化
        orderTables = JSON.parse(orderTables);
        goodsMaps = JSON.parse(goodsMaps);
        let tmpOrdersTable = {
            user: {
                username: orderInfo.content.username,
                phone: orderInfo.content.phone,
                school: orderInfo.content.school,
                address: orderInfo.content.address
            },
            goods_list: []
        };
        // 循环新增map
        orderInfo.goods_list.forEach(item => {
            if ((item === '套包一')) {
                // 夏长裤
                if (goodsMaps.some(mapItem => mapItem.goods_title === '夏长裤 165cm')) { // 已经存在
                    goodsMaps.forEach(mapItem => {
                        if (mapItem.goods_title === '夏长裤 165cm') {
                            mapItem.number += 2
                        }
                    })
                } else { // 不存在，新增
                    goodsMaps.push(
                        {
                            goods_title: '夏长裤 165cm',
                            number: 2,
                            warehouse: '',
                            out_number: '',
                            out_time: '',
                            express_number: ''
                        }
                    )
                }
                // T恤
                if (goodsMaps.some(mapItem => mapItem.goods_title === 'T恤 165cm')) { // 已经存在
                    goodsMaps.forEach(mapItem => {
                        if (mapItem.goods_title === 'T恤 165cm') {
                            mapItem.number += 2
                        }
                    })
                } else { // 不存在，新增
                    goodsMaps.push(
                        {
                            goods_title: 'T恤 165cm',
                            number: 2,
                            warehouse: '',
                            out_number: '',
                            out_time: '',
                            express_number: ''
                        }
                    )
                }
                // 运动短裤
                if (goodsMaps.some(mapItem => mapItem.goods_title === '运动短裤 150cm')) { // 已经存在
                    goodsMaps.forEach(mapItem => {
                        if (mapItem.goods_title === '运动短裤 150cm') {
                            mapItem.number += 3
                        }
                    })
                } else { // 不存在，新增
                    goodsMaps.push(
                        {
                            goods_title: '运动短裤 150cm',
                            number: 3,
                            warehouse: '',
                            out_number: '',
                            out_time: '',
                            express_number: ''
                        }
                    )
                }
            } else if (item === '套包二') {
                // 寸衫
                if (goodsMaps.some(mapItem => mapItem.goods_title === '寸衫 175cm')) { // 已经存在
                    goodsMaps.forEach(mapItem => {
                        if (mapItem.goods_title === '寸衫 175cm') {
                            mapItem.number += 3
                        }
                    })
                } else { // 不存在，新增
                    goodsMaps.push(
                        {
                            goods_title: '寸衫 175cm',
                            number: 3
                        }
                    )
                }
                // 网袜
                if (goodsMaps.some(mapItem => mapItem.goods_title === '网袜')) { // 已经存在
                    goodsMaps.forEach(mapItem => {
                        if (mapItem.goods_title === '网袜') {
                            mapItem.number += 20
                        }
                    })
                } else { // 不存在，新增
                    goodsMaps.push(
                        {
                            goods_title: '网袜',
                            number: 20,
                            warehouse: '',
                            out_number: '',
                            out_time: '',
                            express_number: ''
                        }
                    )
                }
            } else {
                // 运动短裙
                if (goodsMaps.some(mapItem => mapItem.goods_title === '运动短裙')) { // 已经存在
                    goodsMaps.forEach(mapItem => {
                        if (mapItem.goods_title === '运动短裙') {
                            mapItem.number += 10
                        }
                    })
                } else { // 不存在，新增
                    goodsMaps.push(
                        {
                            goods_title: '运动短裙',
                            number: 10,
                            warehouse: '',
                            out_number: '',
                            out_time: '',
                            express_number: ''
                        }
                    )
                }
                // 内胆
                if (goodsMaps.some(mapItem => mapItem.goods_title === '内胆')) { // 已经存在
                    goodsMaps.forEach(mapItem => {
                        if (mapItem.goods_title === '内胆') {
                            mapItem.number += 2
                        }
                    })
                } else { // 不存在，新增
                    goodsMaps.push(
                        {
                            goods_title: '内胆',
                            number: 2,
                            warehouse: '',
                            out_number: '',
                            out_time: '',
                            express_number: ''
                        }
                    )
                }
            }

            // 组装用户订单信息
            if (item === '套包一') {
                tmpOrdersTable.goods_list.push(
                    {
                        goods_title: '夏长裤 165cm',
                        number: 2
                    },
                    {
                        goods_title: 'T恤 165cm',
                        number: 2
                    },
                    {
                        goods_title: '运动短裤',
                        number: 3
                    }
                )
            } else if (item === '套包二') {
                tmpOrdersTable.goods_list.push(
                    {
                        goods_title: '寸衫 175cm',
                        number: 3
                    },
                    {
                        goods_title: '网袜',
                        number: 20
                    }
                )
            } else {
                tmpOrdersTable.goods_list.push(
                    {
                        goods_title: item,
                        number: 10
                    }
                )
            }
        })
        orderTables.push(tmpOrdersTable);

        // 重新写入本地
        localStorage.setItem('goodsMaps', JSON.stringify(goodsMaps));
        localStorage.setItem('orderTables', JSON.stringify(orderTables));
    }

    MessagePlugin.success({
        content: '您已下单成功'
    });

    setTimeout(() => {
        router.replace('/');
    }, 3000)
}

// 停止预售
const endSales = () => {
    isOpen.value = false
}
</script>

<style scoped></style>